import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

/**
 * <p>
 * 代码生成器演示
 * </p>
 */
public class MpGenerator {

    /**
     * 输出路径
     */


    /**
     *           url: jdbc:mysql://172.16.81.253:13306/campus_base_info_dev?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
     *           username: education
     *           password: Chanhu001!
     */
    final static String outPath = "E:\\Generator_Out";
    // 数据库地址
    final static String url =
//        "jdbc:mysql://172.16.141.247:23306/city_pj?characterEncoding=UTF-8&useUnicode=true&useSSL=false";
    "jdbc:mysql://127.0.0.1:3306/wvp?useUnicode=true&characterEncoding=UTF8&rewriteBatchedStatements=true&serverTimezone=PRC&useSSL=false&allowMultiQueries=true";
//    "jdbc:mysql://172.16.159.94:3306/cms_test?characterEncoding=UTF-8&useUnicode=true&useSSL=false";
//        "jdbc:mysql://172.16.141.247:23306/dsq?useUnicode=true&characterEncoding=UTF-8";
//        "jdbc:mysql://192.168.139.251:3306/spring-activiti?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai";
//    "jdbc:mysql://172.16.81.253:13306/campus_educational_info_dev?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";
    // 用户名
//            "jdbc:mysql://172.21.16.2:3306/donggang-city?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";
    final static String username =
            "root"
//    "root";
//            "citypj";
//            "ddsw";
//    "education";
//            "donggang-city";
    ;
    // 密码
    final static String password =
            "123456"
//           "Panjin@2020"
//        "XU2KG2$sNrJrz5Y&9N";
//            "MSUCgWBHDji%M%h8dM";
//            "G7@pJKoGp@Q2DL9qPk";
//    "Chanhu001!";
//            "donggang@xU7SH2d53t%gwM0O9";
    ;
    // 作者 
    final static String author = "huozhenbin";
    // 父级包名
    final static String parentPackage =
            "top.panll.assist"
//            "org.jeecg.modules.manage"
//            "org.jeecg.modules.cms"
//"com.unicom.proj"
            ;
    // 表名数组
    // final static String[] includeTables = {"auth_user_role","farmer_base_info","auth_user_info","auth_role"};
    final static String[] includeTables = {
//"demo"
//
//            "t_md_user_basic_info","t_md_user_role","t_md_role"
//            "t_md_function","t_md_function_classify","t_md_host_function"
//            "t_lj_evaluation_entrance","t_lj_evaluation_info","t_lj_evaluation_model","t_lj_evaluation_model_form","t_lj_evaluation_result"
"record"
    };

    final static String[] excludeTables = {};
    // 表名前缀数组
    final static String[] tablePrefix = {
//            "t_pc"
//            "t_pc"
    };

    public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(username);
        dsc.setPassword(password);
        dsc.setUrl(url);
        autoGenerator.setDataSource(dsc);
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(outPath);
        gc.setOpen(true);
        gc.setFileOverride(true);
        // ActiveRecord特性
        gc.setActiveRecord(false);
        // XML二级缓存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(false);
        // XML columList
        gc.setBaseColumnList(true);
        // 作者
        gc.setAuthor(author);
        // Swagger2
        gc.setSwagger2(true);
        // 日期类型
        gc.setDateType(DateType.ONLY_DATE);
        // 主键策略
        gc.setIdType(IdType.ID_WORKER);

        // 自定义文件命名，注意%s 会自动填充表实体属性
        gc.setControllerName("%sController");
        gc.setServiceName("I%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setEntityName("%sEntity");
        autoGenerator.setGlobalConfig(gc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // 此处可以修改您的表前缀
        // strategy.setTablePrefix(tablePrefix);
        // 表名生成策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        if (includeTables.length > 0) {
            // 需要生成的表
            strategy.setInclude(includeTables);
        } else {
            if (excludeTables.length > 0) {
                // 需要排除的表
                strategy.setExclude(excludeTables);
            }
        }
        // 表名前缀
        strategy.setTablePrefix(tablePrefix);
        strategy.setRestControllerStyle(true);
        strategy.setEntityLombokModel(true);
        // strategy.setSuperServiceClass(null);
        // strategy.setSuperServiceImplClass(null);
        strategy.setSuperMapperClass(null);
        strategy.setEntityTableFieldAnnotationEnable(true);
        autoGenerator.setStrategy(strategy);
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(parentPackage);
        // pc.setController("controller");
        // pc.setService("service");
        // pc.setServiceImpl("service.impl");
        // pc.setMapper("mapper");
        // pc.setEntity("entity");
        // pc.setXml("mapper.xml");
        autoGenerator.setPackageInfo(pc);
        // 自定义属性注入 没有这个不报错 但是生成失败
        InjectionConfig injectionConfig = new InjectionConfig() {
            // 自定义属性注入:abc
            // 在.ftl(或者是.vm)模板中，通过${cfg.abc}获取属性
            @Override
            public void initMap() {
                // Map<String, Object> map = new HashMap<>();
                // map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                // this.setMap(map);
            }
        };
        // 配置自定义属性注入
        autoGenerator.setCfg(injectionConfig);
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
//        autoGenerator.setTemplateEngine(new VelocityTemplateEngine());
        // 执行生成
        autoGenerator.execute();
    }

}
